package com.enjoyor.soa.traffic.util.helper;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.*;

public final class JsonHelper {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public synchronized static <T> Object jsonToObject(String json,
			Class valueClz) {
		T object = null;
		try {
			object = (T) JSON.parseObject(json, valueClz);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return object;
	}

	/**
	 * javaBean转jsonstr
	 * @author wulei
	 * @param javaBean(DTO、POJO等)
	 * @return JSONSring
	 */
	public synchronized static String beanToJsonStr(Object javaObject) {
		
		String string = "";
		try {
			JSONObject object = (JSONObject)JSON.toJSON(javaObject);
			string = object.toJSONString();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return string;
	}

	public synchronized static String ObjsToJsonStr(Object[] objs) {

		String[] strs =new String[objs.length];
		String string="";
		try {
			for(int i=0;i<strs.length;i++) {
				JSONObject object = (JSONObject) JSON.toJSON(objs[i]);
				strs[i] = object.toJSONString();
			}
			string=JSON.toJSONString(Arrays.toString(strs));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return string;
	}
	
	public synchronized static String listToJsonStr(List list){
		String string = "";
		try {
			string = JSON.toJSONString(list);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return string;
	}
	
	public synchronized static String getJsonFiled(String json, String key) {
		String value = "";
		JSONObject jsonObject = JSONObject.parseObject(json);
		value = jsonObject.get(key) != null ? jsonObject.getString(key) : "";
		return value;
	}

	/**
	 * TODO 获取参数
	 * 
	 * @author zhanghuabo
	 * @param 页面传递的jsonstr
	 * @return 参数map
	 * @throws Exception 
	 */
	public synchronized static Map<String, Object> jsonStrToMap(String data) throws Exception {
		try{
			JSONObject dataJson = JSONObject.parseObject(data);
			return JsonHelper.jsonToMap(dataJson);
		}catch(Exception ex){
			//LoggerHelper.LOG.error("jsonStrToMap调用异常!", ex);
			throw ex;
		}
	}

	public synchronized static Map<String, Object> jsonToMap(
			JSONObject jsonObject) throws Exception {
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			if (jsonObject != null && !jsonObject.equals("")) {
				Set<String> keySet = jsonObject.keySet();
				Iterator<String> it = keySet.iterator();
				while (it.hasNext()) {
					String key = (String) it.next();
					String value = jsonObject.getString(key);
					map.put(key, value);
				}
			}
			return map;
		} catch (Exception ex) {
			//LoggerHelper.LOG.error("jsonToMap调用异常!", ex);
			throw ex;
		}
	}

	public synchronized static JSONObject mapToJson(Map<String, Object> map) {
		JSONObject jsonObject = new JSONObject();
		if (map != null) {
			Set<String> keySet = map.keySet();
			Iterator<String> it = keySet.iterator();
			while (it.hasNext()) {
				String key = it.next();
				jsonObject.put(key, map.get(key));
			}
		}
		return jsonObject;
	}
	
	public static boolean isJsonArray(JSONObject json,String node){
		if(null != json){
			Object obj = json.get(node);
			if(obj instanceof JSONArray){
				return true;
			}else{
				return false;
			}
		}
		return false;
	}

}
